/*
 *翻页控件封装
 *调用方式如下
 * var page = new PageControl();
 * page.init({
 *         totalCount:15,    //总页数
 *         conter:".app",    //容器选择器
 *         nowPage:1,        //当前页，默认第几页
 *         url:"localhost:5331", //接口请求api
 *         data:{                //api传参，未接入扩展参数
 *             text:11
 *         }
 *     });
 */
"use strict";
var PageControl = function () {
    //实例化对象，可以多次引用
    var result = {
        conter: "body",
        totalCount: 0,
        nowPage: 1,
        url: '',
        data: null,
        callback: null,
        init: function (obj) {
            var obj = obj?obj:{};
            //接受传参
            this.conact(obj);
            //初始化控件
            this.renderTemplate();
        },
        conact: function (obj) {
            for (var key in obj) {
                this[key] = obj[key];
            }
        },
        renderTemplate: function () {
            //清空所有
            $(this.conter).children().remove();
            var _html = '';
            _html += '<ul class="ppm-page-list">\
            <li data-num="-3">首页</li>\
            <li data-num="-1">上一页</li>';
            if (this.totalCount <= 9) {
                for (var i = 1; i <= this.totalCount; i++) {
                    _html += '<li data-num="' + i + '">' + i + '</li>';
                }
            } else {
                if (this.nowPage <= 5) {
                    for (var i = 1; i <= this.totalCount; i++) {
                        if (i <= 9) {
                            _html += '<li data-num="' + i + '">' + i + '</li>';
                        } else {
                            _html += '<span class="ppm-page-omit">···</span>';
                            break;
                        }
                    }
                } else if (this.nowPage >= (this.totalCount - 4)) {
                    for (var i = (this.totalCount - 8); i <= this.totalCount; i++) {
                        _html += '<li data-num="' + i + '">' + i + '</li>';
                    }
                } else {
                    for (var i = this.nowPage - 4; i <= this.totalCount; i++) {
                        if (i <= this.nowPage + 4) {
                            _html += '<li data-num="' + i + '">' + i + '</li>';
                        } else {
                            _html += '<span class="ppm-page-omit">···</span>';
                            break;
                        }
                    }
                }
            }
            _html += '<li data-num="-2">下一页</li>\
            <li data-num="-4">尾页</li>\
        </ul>';
            $(this.conter).append(_html);
            this.addEvent(); //动态生成标签，需重绑demo树的事件
            this.showLight();
        },
        showLight: function () {
            //显示对应的标签
            $(this.conter).find('li').removeClass('ppm-active');
            $(this.conter).find('li[data-num=' + this.nowPage + ']').addClass("ppm-active");
        },
        addEvent: function () {
            var self = this;
            $(this.conter).find("li").off('click').on('click', function () {
                var _cur = $(this);
                var pagenum = parseInt(_cur.attr("data-num"));
                switch (pagenum) {
                    case -4:
                        self.lastPage();
                        break;
                    case -3:
                        self.firstPage();
                        break;
                    case -2:
                        self.nextPage();
                        break;
                    case -1:
                        self.prePage();
                        break;
                    default:
                        self.nowPage = pagenum;
                        self.goTo();
                        break;
                };
            });
        },
        nextPage: function () {
            //下一页
            if (this.nowPage == this.totalCount) {
                alert("已是最后一页");
                return;
            } else {
                this.nowPage++;
                //调用查询数据接口
                this.renderTemplate();
                typeof(this.callback) == "function" && this.callback(this.nowPage);
            }
        },
        prePage: function () {
            if (this.nowPage == 1) {
                alert("已是第一页");
            } else {
                this.nowPage--;
                //调用查询数据接口
                this.renderTemplate();
                typeof(this.callback) == "function" && this.callback(this.nowPage);
            }
        },
        firstPage: function () {
            this.nowPage=1;
            //调用查询数据接口
            this.renderTemplate();
            typeof(this.callback) == "function" && this.callback(this.nowPage);
        },
        lastPage:function(){
            this.nowPage=this.totalCount;
            //调用查询数据接口
            this.renderTemplate();
            typeof(this.callback) == "function" && this.callback(this.nowPage);
        },
        goTo: function () {
            //调用查询数据接口
            this.renderTemplate();
            typeof(this.callback) == "function" && this.callback(this.nowPage);
        },
        post: function () {
            //暂时废弃
            $.ajax({
                url: this.url,
                type: JSON,
                type:'POST',
                data: this.data,
                async: true,
                success: function (result, status, xhr) {
                    //成功调用回掉函数
                   
                },
                error: function () {
                    //错误回掉
                    alert('接口异常，请稍后重试！');
                }
            });
        }
    }
    return result;
}